package com.fbty.mt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.directwebremoting.WebContextFactory;

import com.fbty.base.dao.MtJdbcDao;
import com.fbty.base.domain.Page;
import com.fbty.mt.dao.RechargeDao;
import com.fbty.mt.dao.UserDao;
import com.fbty.mt.domain.Recharge;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.RechargeService;
/**
 * 购买服务类
 * @author thinkpad
 *
 */
public class RechargeServiceImpl implements RechargeService{
	private MtJdbcDao mtJdbcDao;
	private RechargeDao rechargeDao;
	private UserDao userDao;
	/**
	 * 保存购买记录
	 * @param num
	 * @param price
	 * @param time
	 */
	@Override
	public void saveRecharge(Recharge recharge,User user) {
		rechargeDao.save(recharge);
		userDao.update(user);
	}
	/**
	 * 显示价格
	 * @param pids
	 * @return
	 */
	public List showPrice(String roleId) {
		String sql = "";
		if("".equals(roleId)||null == roleId) {
			sql = " select * from tab_price order by sort ";
		} else {
			sql = " select * from tab_price where type=? order by price ";
		}
		System.out.println(sql);
		Object[] params = {roleId};
		List<Map> list = mtJdbcDao.getJdbcTemplate().queryForList(sql,params);
		List<Map> list2 = new ArrayList<Map>();
		for(int i=0; i<list.size(); i++) {
			Map map = new HashMap();
			map.put("id", list.get(i).get("id"));
			map.put("num", list.get(i).get("num"));
			map.put("price", list.get(i).get("price"));
			if("2".equals(roleId)) {
				map.put("date", list.get(i).get("time"));
				map.put("time", list.get(i).get("time"));
			} else {
				map.put("date", list.get(i).get("time"));
				map.put("time", list.get(i).get("time"));
			}
			map.put("roleId", roleId);
			list2.add(map);
		}
		return list2;
	}
	/**
	 * 分页显示购买记录
	 * @param pids
	 * @return
	 */
	public Page pageList(Integer pageNo,Integer pageSize,Map<String,String> parms) {
		String sql = " SELECT r.*,tu.username,tu.name,tr.title as roleName,tr.id as rid, DATE_FORMAT(DATE,'%Y-%m-%d %H:%i:%s') AS buyTime "+
					 " FROM tab_recharge r LEFT JOIN tab_user tu ON r.uid = tu.id " +
					 " left join user2role ur on tu.id=ur.uid " +
					 " left join tab_role tr on tr.id= ur.rid where 1=1 ";
		List list = new ArrayList();
		if(parms.get("roleId") != null && !"".equals(parms.get("roleId"))) {
			sql += " and tr.id=? ";
			list.add(parms.get("roleId"));
		}
		if(parms.get("start") != null && !"".equals(parms.get("start"))) {
			sql += " and DATE_FORMAT(r.date,'%Y-%m-%d') > ? ";
			list.add(parms.get("start"));
		}
		if(parms.get("end") != null && !"".equals(parms.get("end"))) {
			sql += " and r.date <= ? ";
			list.add(parms.get("end"));
		}
		if(parms.get("username") != null && !"".equals(parms.get("username"))) {
			sql += " and tu.username like ? ";
			list.add(parms.get("username"));
		}
		if(parms.get("name") != null && !"".equals(parms.get("name"))) {
			sql += " and tu.name like ? ";
			list.add(parms.get("name"));
		}
		Object[] objs = list.toArray();
		List<Map> list2 = mtJdbcDao.getJdbcTemplate().queryForList(sql,objs);
		Integer count = list2.size();
		return new Page(list2, pageNo*pageSize, count, pageSize);
	}
	/**
	 * 删除购买记录
	 * @param pids
	 * @return
	 */
	public void deleteRecharge(String id) {
		rechargeDao.removeById(Integer.parseInt(id));
	}
	public MtJdbcDao getMtJdbcDao() {
		return mtJdbcDao;
	}
	public void setMtJdbcDao(MtJdbcDao mtJdbcDao) {
		this.mtJdbcDao = mtJdbcDao;
	}
	public RechargeDao getRechargeDao() {
		return rechargeDao;
	}
	public void setRechargeDao(RechargeDao rechargeDao) {
		this.rechargeDao = rechargeDao;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
